<?php
namespace App\Traits;

use Illuminate\Http\Request;

trait CommonData{
	// 获取近几个月的日期
    public function getDateByDay($day)
    {
        $currentTime = time();
        $cyear = floor(date("Y",$currentTime));
        $cMonth = floor(date("m",$currentTime));
        $d = [];
        for($i=0;$i<$day;$i++){
            $nMonth = $cMonth-$i;
            $cyear = $nMonth == 0 ? ($cyear-1) : $cyear;
            $nMonth = $nMonth <= 0 ? 12+$nMonth : $nMonth;
            $date = $cyear."-".$nMonth."-1";
            $firstday = date('Y-m-01', strtotime($date));
            $lastday = date('Y-m-t', strtotime($date));
            $d['m'][] = $nMonth;
            $d['s'][] = $firstday;
            $d['e'][] = $lastday;
        }
        sort($d['m']);
        sort($d['s']);
        sort($d['e']);
        return $d;
    }
    /**
     * 根据经纬度和半径计算出范围
     * @param string $lat 纬度
     * @param String $lng 经度
     * @param float $radius 半径 单位米
     * @return Array 范围数组
     */
    public function calcScope($lat, $lng, $radius) {
        define("PI", "3.1415");
        $degree = (24901*1609)/360.0;
        $dpmLat = 1/$degree;

        $radiusLat = $dpmLat*$radius;
        $minLat = $lat - $radiusLat;    // 最小纬度
        $maxLat = $lat + $radiusLat;    // 最大纬度

        $mpdLng = $degree*cos($lat * (PI/180));
        $dpmLng = 1 / $mpdLng;
        $radiusLng = $dpmLng*$radius;
        $minLng = $lng - $radiusLng;   // 最小经度
        $maxLng = $lng + $radiusLng;   // 最大经度

        /** 返回范围数组 */
        $scope = array(
            'minLat'  => $minLat,
            'maxLat'  => $maxLat,
            'minLng'  => $minLng,
            'maxLng'  => $maxLng
        );
        return $scope;
    }

}